package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;


import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.data.GetDockData;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.json.coordinationjson;
import com.example.yiwei.crm.json.dockJson;
import com.example.yiwei.crm.togglebutton.ToggleButton;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.RunnableFuture;

public class CoorDelectActivity extends AppCompatActivity {
    private ToggleButton mToggleButton01;
    int id ;
    int gz;
    ImageView im_back;
    String result;
    Button delect_btn;
    SharedPreferences sp;
   dockingEntity dockingEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coor_delect);
        mToggleButton01 = (ToggleButton) findViewById(R.id.togg_btn);
        id = getIntent().getIntExtra("id",0);
        gz = getIntent().getIntExtra("zt",0);
        im_back = (ImageView) findViewById(R.id.detailed_dock_img_back);
        delect_btn = (Button) findViewById(R.id.delect_btn);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        delect_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder;
                builder = new AlertDialog.Builder(new ContextThemeWrapper(CoorDelectActivity.this, R.style.custom_dialog));
                builder.setTitle("您确定要删除吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("DEL01", 1);
                                    object.put("KXA01", id);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                String json = object.toString();
                                result = NetWorkUtils.getCount(baseClass.delect_coordianation_URL, json);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            JSONObject object1 = new JSONObject(result);
                                            boolean b = object1.getBoolean("ifsuccess");
                                            if (b) {
                                                DetailedCoordinationActivity.a.finish();
                                                finish();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        }).start();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();
            }
        });
        im_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject object =new JSONObject();
                        try {
                            object.put("KXA01",id);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        dockingEntity= coordinationjson.jsonCorrdiantiondockMessage1(baseClass.find_coording_message_url, json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (dockingEntity!=null){
                                    GetDock.dockingEntity=dockingEntity;
                                    if ( GetDock.dockingEntity!=null){
                                        finish();
                                    }
                                }
                            }
                        });
                    }
                }).start();
            }
        });
        Log.d("aa",gz+"");
        if (gz==1){
            mToggleButton01.setToggleOn();
        }else {
            mToggleButton01.setToggleOff();
        }
        mToggleButton01.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            NetWorkUtils.getCount(baseClass.base_url+"/appCooperation/updateStarValue"+"?KXA01="+id+"&flag=1",null);

                        }
                    }).start();
                }else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            NetWorkUtils.getCount(baseClass.base_url+"/appCooperation/updateStarValue"+"?KXA01="+id+"&flag=0",null);
                        }
                    }).start();
                }
            }
        });
    }
}
